/**
 * Created by maiseries on 2017/9/25.
 */
import React from 'react';

class Format extends React.Component {
    /**
     * 格式化money
     * s为要格式化的money
     * n为小数位数
     */
    static money(s, n) {
        if (s === '' || !s)
            return s;
        n = n > 0 && n <= 20 ? n : 3;
        s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
        s = s.substring(0, s.lastIndexOf('.') + n) // 123456.78
        const l = s.split(".")[0].split("").reverse(),
            r = s.split(".")[1];
        let t = "";
        for (let i = 0; i < l.length; i++) {
            t += l[i] + ((i + 1) % 3 === 0 && (i + 1) !== l.length ? "," : "");
        }
        return t.split("").reverse().join("") + "." + r;
    }

    static subStr(s, n) {
        if (s && typeof s === 'string' && s === '')
            return;
        n = n > 0 ? n : 15;
        return s.length > n ? s.substring(0, n - 1) + '...' : s;
    }

    static bankCard(s) {
        if (s === '')
            return;
        let r = s.substring(0, 4) + '  ****  ****  ' + s.substring(12, 16);
        if (s.length > 16) {
            r += '  ' + s.substring(16, 18)
        }
        return r;
    }

    static phone(s) {
        if (s === '')
            return;
        return s.substring(0, 3) + '****' + s.substring(7, 11);
    }

    static date(s) {
        if (s === '')
            return;
        s = s + '';
        return s.substring(0, 10).replace(/\-/g, ".");
    }

    static date_mmdd(s) {
        if (s === '')
            return;
        s = s + '';
        return s.substring(5, 10).replace('-', '.');
    }

    static industry(arr) {
        if(arr && arr.length > 0){
            return arr.map(item=>item.label);
        }else {
            return [];
        }

    }

    static asset(arr) {
        if(arr && arr.length > 0){
            return arr.filter(pItem=>pItem.asset_grade === 2).map(item=>{
                return {
                    ...item,
                    children: arr.filter(cItem=>cItem.parent_id === item.id)
                }
            })
        }else {
            return []
        }
    }

    static region(arr) {
        if(arr && arr.length > 0){
            return arr.filter(item=>item.level === 1).sort((o1,o2)=>o1.value < o2.value ? -1 : 1).map(_province=>{
                let province = {};
                province[_province.label] = arr.filter(item1=>item1.pcode === _province.value).map(_city=>{
                    let city = {};
                    city[_city.label] = arr.filter(item2=>item2.pcode === _city.value).map(_district=>_district.label)
                    return city;
                })
                return province;
            })
        }else {
            return [];
        }
    }

    static message(arr) {

    }

}

module.exports = Format;
